package com.ruicar.afs.cloud.manage.common.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.admin.api.dto.UserSimpleInfoDto;
import com.ruicar.afs.cloud.admin.api.feign.AfsUserFeign;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import com.ruicar.afs.cloud.manage.common.condition.MessageTemplateCondition;
import com.ruicar.afs.cloud.manage.common.entity.ManageSmsTemplate;
import com.ruicar.afs.cloud.manage.common.service.ManageSmsTemplateService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 短信、邮件模板操作
 */
@Slf4j
@RestController
@RequestMapping("/smsTemplate")
@AllArgsConstructor
public class ManageSmsTemplateController {

    private final ManageSmsTemplateService manageSmsTemplateService;
    private final AfsUserFeign afsUserFeign;

    @PostMapping(value = "/addTemplate")
    @ApiOperation(value = "新增短信模板")
    @Transactional
    @SysLog("新增短信模板")
    public IResponse<Boolean> save(@RequestBody ManageSmsTemplate manageSmsTemplate) {
        String templateId = manageSmsTemplate.getTemplateId();
        List<ManageSmsTemplate> templates = manageSmsTemplateService.list(Wrappers.<ManageSmsTemplate>query().lambda().eq(ManageSmsTemplate::getTemplateId, templateId));
        if (EmptyUtils.isNotEmpty(templates)) {
            return new IResponse<Boolean>().setMsg("模板ID:" + templateId + "重复,请重新提交！");
        } else {
            if(manageSmsTemplateService.save(manageSmsTemplate)){
                return new IResponse().success("新增成功");
            }else{
                return new IResponse().fail("新增失败");
            }
        }

    }

    @RequestMapping(value = "/deleteTemplate/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value = "通过id删除")
    @Transactional
    @SysLog("删除短信模板")
    public IResponse<Boolean> del(@PathVariable String id) {
        if(manageSmsTemplateService.removeById(id)){
            return new IResponse().success("删除成功");
        }else{
            return new IResponse().fail("删除失败");
        }
    }

    @PostMapping(value = "/editTemplate")
    @ApiOperation(value = "修改短信模板")
    @Transactional
    @SysLog("修改短信模板")
    public IResponse<Boolean> edit(@RequestBody ManageSmsTemplate manageSmsTemplate) {
        if(manageSmsTemplateService.updateById(manageSmsTemplate)){
            return new IResponse().success("修改成功");
        }else{
            return new IResponse().fail("修改失败");
        }
    }

    @PostMapping(value = "/queryTemplateList")
    @ApiOperation(value = "多条件分页获取信审短信模板信息数据")
    public IResponse<IPage<ManageSmsTemplate>> query(@RequestBody QueryCondition<ManageSmsTemplate> condition) {
        return IResponse.success(manageSmsTemplateService.page(new Page(condition.getPageNumber(), condition.getPageSize()), Wrappers.<ManageSmsTemplate>query().lambda()
                .eq(EmptyUtils.isNotEmpty(condition.getCondition().getTemplateType()),ManageSmsTemplate::getTemplateType,condition.getCondition().getTemplateType())
                .eq(EmptyUtils.isNotEmpty(condition.getCondition().getStatus()),ManageSmsTemplate::getStatus,condition.getCondition().getStatus())
        ));
    }


    @PostMapping(value = "/sendMessageInfo")
    @ApiOperation(value = "信息发送接口")
    @Transactional
    @SysLog("短信发送")
    public IResponse<Boolean> sendMessageInfo(@RequestBody @Validated MessageTemplateCondition condition) {
        return manageSmsTemplateService.sendMsg(condition);
    }

    @PostMapping(value = "/getEmail")
    @ApiOperation(value = "获取用户邮箱")
    @SysLog("获取用户邮箱")
    public IResponse<Boolean> getEmail(@RequestBody QueryCondition<UserSimpleInfoDto> var) {
        IResponse<Page<UserSimpleInfoDto>> pageIResponse = afsUserFeign.pageQuerySimpleUserInfo(var);
        return new IResponse().setData(pageIResponse);
    }


}
